﻿using System;
using System.ComponentModel;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Windows.Data;
using Simhopp.Common;
using System.Collections.Generic;
namespace Simhopp.Client
{ 
    /// <summary>
    /// Struct to handle data for current diver, in a collected way.
    /// </summary>
    public struct NowJumpingDataStruct : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private Diver diver;
        private Rounds round;
        public String Name
        {
            get
            {
                return diver.Name;;
            }
        }
        public String Nationality
        {
            get
            {
                return diver.Nationality;
            }
        }
        public String Type
        {
            get
            {
                if (round != Rounds.Configure)
                {
                return diver.Dives[(int)round].TypeName;
            }
                return "";
            }
        }

        public int CurrentHeight
        {
            get 
            {
                if (round != Rounds.Configure)
                {
                    return (int)diver.Dives[(int)round].Height;
                }
                return 0;
            }
        }

        public int CurrentRound
        {
            get
            {
                return (int)round + 1;
            }
        }

        public void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }


        public NowJumpingDataStruct(Simhopp.Common.Diver diver, Rounds round)
            : this()
        {
            this.diver = diver;
            this.round = round;
        }

        public void Update(Diver diver, Rounds round)
        {
            this.diver = diver;
            this.round = round;
            NotifyPropertyChanged(String.Empty);
    }
    }
    /// <summary>
    /// Converts a double to a string on the format 0.00, and any string to a double.
    /// </summary>
    public class DoubleToStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType,
        object parameter, CultureInfo culture)
        {
            return String.Format("{0:0.00}", value);
        }

        public object ConvertBack(object value, Type targetType,
            object parameter, CultureInfo culture)
        {
            var str = value as String;

            if (str == null)
            {
                return String.Empty;
            }

            return double.Parse(str);
        }
    }

    /// <summary>
    /// Converts an integer to a string, and a string to an integer.
    /// </summary>
     public class IntegerToStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType,
        object parameter, CultureInfo culture)
        {
            return String.Format("{0}", value);
        }

        public object ConvertBack(object value, Type targetType,
            object parameter, CultureInfo culture)
        {
            var str = value as String;

            if (str == null)
            {
                return String.Empty;
            }

            return int.Parse(str);
        }
    }
    /// <summary>
    /// KeyValuePair implementation with mutable properties. Can be created with none, or both of the parameters as reference parameters by using a specific constructor.
    /// </summary>
    /// <typeparam name="TKey">The type of the key</typeparam>
    /// <typeparam name="TValue">The type of the value</typeparam>
     public class MutableKeyValuePair<TKey, TValue>
     {
         public TKey Key { get; set; }
         public TValue Value { get; set; }
         /// <summary>
         /// Creates a new MutableKeyValuePair<Tkey, TValue> with two parameters passed using standard convention.
         /// </summary>
         /// <param name="key">The key</param>
         /// <param name="value">The value</param>
         public MutableKeyValuePair(TKey key, TValue value)
         {
             this.Key = key;
             this.Value = value;
         }

         /// <summary>
         /// Creates a new MutableKeyValuePair<Tkey, TValue> with two parameters passed as references.
         /// </summary>
         /// <param name="key">The key reference</param>
         /// <param name="value">The value reference</param>
         public MutableKeyValuePair(ref TKey key, ref TValue value)
         {
             this.Key = key;
             this.Value = value;
         }


         /// <summary>
         /// Creates a new MutableKeyValuePair<Tkey, TValue> with the key as a reference and the value passed with standard convention.
         /// </summary>
         /// <param name="key">The key reference</param>
         /// <param name="value">The value</param>
         public MutableKeyValuePair(ref TKey key, TValue value)
         {
             this.Key = key;
             this.Value = value;
         }

         /// <summary>
         /// Creates a new MutableKeyValuePair<Tkey, TValue> with the key passed using standard convention and the value as a reference.
         /// </summary>
         /// <param name="key">The key </param>
         /// <param name="value">The value reference</param>
         public MutableKeyValuePair(TKey key, ref TValue value)
         {
             this.Key = key;
             this.Value = value;
         }
     }

    /// <summary>
    /// Converts an object to bool, using the convention that null == false. @warning the ConvertBack is always null!
    /// </summary>
    public class ObjectToBoolConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null){
                return false;
            }
            return true;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

}